const express = require('express');
const cors = require('cors');
const jwt = require("jsonwebtoken");
const JWT_SECRET_KEY = "JWT_SECRET_KEY";
const Response = require("./utils/Response.js");
const cookieParser = require('cookie-parser');
const session = require('express-session');

const Mail = require('./routers/mail');
const Phone = require('./routers/phone');
const User = require('./routers/user');
const Works = require('./routers/works');
const Draw = require('./routers/draw');
const Upload = require('./routers/upload');
const essay = require('./routers/eassay');
const admin = require('./routers/admin'); 
const west = require('./routers/westDraw');
const commodity = require('./routers/commodity');
const order = require('./routers/order')

const { use } = require('./routers/mail');
const app = express();

app.use(cookieParser('keyboard cat'));

app.use(express.urlencoded({
    extended:true
}))

app.use(session({
    nickname: "user_key",
    secret: 'keyboard cat',
    resave: false,
    saveUninitialized: true,
    duration: 30 * 60 * 1000,
    activeDuration: 5 * 60 * 1000
}));



// app.use('/', function (req, res) {
 
//     //假设使用count记录用户访问的次数
//    if(req.session['count'] == null) {
//        req.session['count'] = 1;
//    }else{
//        req.session['count']++;
//    }
//    console.log(req.session['count'])
//     res.send('ok')
// })

app.listen('4100',()=>{
    console.log('启动成功')
})

app.use(
    cors({
      origin: "*",
    })
);


app.use('/v1/Mail',Mail);
app.use('/v1/Phone',Phone);
app.use('/v1/User',User);
app.use('/v1/Works',Works);
app.use('/v1/Draw',Draw);
app.use('/v1/Upload',Upload);
app.use('/v1/Eassay',essay);
app.use('/v1/Admin',admin);
app.use('/v1/WestDraw',west);
app.use('/v1/Commodity',commodity);
app.use('/v1/Order',order);


app.use(function (req, res, next) {
    res.status(404);
    res.render('error');
});


app.use((err,req,res,next)=>{
    console.log(err);
    res.send({code:500,message:'服务器错误'});
    res.locals.message = err.message;

    res.locals.error = req.app.get('env') === 'development' ? err : {};

    res.status(err.status || 500);

    res.render('error');
})